package com.cgnpc.scp.manager.controller;

import com.cgnpc.cud.annotation.OperatorType;
import com.cgnpc.cud.annotation.UBA;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.AjaxResultUtil;
import com.cgnpc.scp.manager.mybatis.entity.DataDict;
import com.cgnpc.scp.manager.service.IDataDictService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;

/******************************
 * 用途说明: 数据字典Controller类<br>
 *     Controller类的请求地址用"/服务/模块/类"，例如"/scp服务/data数据模块/dataDict数据字典类"，"/scp/data/dataDict"<br>
 * 作者姓名: yblrs
 * 创建时间: 2020/8/16 17:39
 ******************************/
@Controller
@RequestMapping("/scp/data/dataDict")
public class DataDictController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataDictController.class);

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private IDataDictService dataDictService;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }
    private void i18nTest() {
        // begin i18测试
        HttpServletRequest request = null;
        AjaxResult ajaxResult = null;

        String i18nKey = "message.cud.test.i18n.message";
        Object[] i18nValue = new Object[]{"女孩"};
        /**
         *
         * @see org.springframework.web.servlet.i18n.LocaleChangeInterceptor#preHandle(HttpServletRequest, HttpServletResponse, Object)
         */
        // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
        Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);
        // 获取对应语言的返回值
        String strMessage = messageSource.getMessage(i18nKey, i18nValue, reqLocale);

        ajaxResult.put(AjaxResultUtil.KEY_MSG, strMessage); // i18测试
        // end i18测试
    }

    /**********************************
     * 用途说明: 数据字典---分页查询
     * 参数说明: HttpServletRequest request, @RequestBody T reqEntity
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "数据字典---分页查询")
    @UBA(module = "系统管理", action = "数据字典分页查询", channel = OperatorType.Page)
    @RequestMapping(value = "/loadByPaging", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends DataDict> Object loadByPaging(HttpServletRequest request, @RequestBody T reqEntity) {
        AjaxResult ajaxResult = null;

        try {
            MyPageResult<T> myPageResult = dataDictService.loadByPaging(reqEntity);

            // begin i18n生产环境
            // 使用i18n时，需要配置message.properties, messages_en_US.properties, messages_zh_CN.properties文件的key, value；
            // 以下为样例：
            // message.cud.test.i18n.message=Hello, test I18N, language for no language, your name: Girl, dynamic name: {0}, second name: {0}, age: {1}.
            // i18nKey:"message.cud.test.i18n.message"；在配置文件中值配置了3个占位符，可以动态填充数据，下标从0开始增长；
            // i18nValues:为值数组；
            String i18nKey = "message.cud.test.i18n.message";
            Object[] i18nValues = new Object[]{"女孩", 20, "实际没有使用的值"};

            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            // @see org.springframework.web.servlet.i18n.LocaleChangeInterceptor#preHandle(HttpServletRequest, HttpServletResponse, Object)
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            // 使用AjaxResultUtil创建返回值
            ajaxResult = AjaxResultUtil.createI18nSuccessMessage(reqLocale, messageSource, i18nKey, i18nValues, myPageResult);
            // end i18n生产环境
        } catch (Exception e) {
            LOGGER.error("loadByPaging.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 数据字典---保存或修改
     * 参数说明: HttpServletRequest request, @RequestBody List<T> reqEntityList
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "数据字典---保存或修改")
    @UBA(module = "系统管理", action = "保存或修改数据字典", channel = OperatorType.Button)
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends DataDict> Object saveOrUpdate(HttpServletRequest request, @RequestBody List<T> reqEntityList) {
        AjaxResult ajaxResult = null;

        try {
            // 获取当前的登录用户信息
            UserInfo loginUserInfo = currentUser.getCurrentUserDetail();
            // 获取请求的语言reqLocale，cookie需要包含lang=cn，或者lang=en
            Locale reqLocale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);

            ajaxResult = dataDictService.saveOrUpdate(loginUserInfo, reqLocale, reqEntityList);
        } catch (Exception e) {
            LOGGER.error("saveOrUpdate.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

    /**********************************
     * 用途说明: 数据字典---查询指定信息
     * 参数说明: HttpServletRequest request, @RequestBody T reqDataDict
     * 返回值说明: Object
     ***********************************/
    @ApiOperation(value = "数据字典---查询指定信息")
    @UBA(module = "系统管理", action = "查看数字字典详情", channel = OperatorType.Button)
    @RequestMapping(value = "/queryByDictGroupKey", method = {RequestMethod.POST})
    @ResponseBody
    public <T extends DataDict> Object queryByDictGroupKey(HttpServletRequest request, @RequestBody T reqDataDict) {
        AjaxResult ajaxResult = null;

        try {
            List<DataDict> myPageResult = dataDictService.queryByDictGroupKey(reqDataDict);
            ajaxResult = AjaxResultUtil.createSuccessMessage(myPageResult);
        } catch (Exception e) {
            LOGGER.error("queryByDictGroupKey.error;", e);
            ajaxResult = AjaxResultUtil.createErrorMessage();
        }

        return ajaxResult;
    }

}
